from rest_framework import serializers

from goods.models import SKU
from orders.models import OrderInfo, OrderGoods


class OrderInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderInfo
        fields = "__all__"


class SKUSerializser(serializers.ModelSerializer):
    class Meta:
        model = SKU
        fields = "__all__"


class OrderGoodsSerializser(serializers.ModelSerializer):
    sku = SKUSerializser(read_only=True)

    class Meta:
        model = OrderGoods
        fields = "__all__"


class OrderDetailSerializer(serializers.ModelSerializer):
    skus = OrderGoodsSerializser(read_only=True, many=True)

    class Meta:
        model = OrderInfo
        fields = "__all__"



class OrderStatusSerializser(serializers.Serializer):
    status = serializers.IntegerField(write_only=True)

    # 重写update
    def update(self, instance, validated_data):
        instance.status = validated_data['status']
        instance.save()
        return instance